﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenerateTagLinks
{
    #region Using
    using System.Xml;
    using System.IO;
    #endregion

    public static class DataProvider
    {
        #region Variables
        public static string InputPath { get; set; }
        #endregion

        #region Methods
        public static bool LoadDBInfo(ref List<string> lstKeywords, ref List<string> lstUrls)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(InputPath);

                XmlElement root = doc.DocumentElement;

                XmlNode keywords = root.ChildNodes[0];
                XmlNode urls = root.ChildNodes[1];

                lstKeywords.Clear();
                lstUrls.Clear();

                foreach (XmlNode childNode in keywords.ChildNodes)
                {
                    lstKeywords.Add(childNode.InnerText);
                }

                foreach (XmlNode childNode in urls.ChildNodes)
                {
                    lstUrls.Add(childNode.InnerText);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string IsExist(string _url, string _keyword = "")
        {
            var lstKeyword = new List<string>();
            var lstUrl = new List<string>();
            if (LoadDBInfo(ref lstKeyword, ref lstUrl))
            {
                if (_keyword != "")
                {
                    if (lstKeyword.Contains(_keyword))
                        return "Keyword da ton tai";
                }

                if (_url != "")
                {
                    if (lstUrl.Contains(_url))
                        return "Url da ton tai";
                }
            }

            return "";
        }

        public static string AddNewNode(string _keyword, string _url)
        {
            var errText = "";
            try
            {
                errText = IsExist(_url, _keyword);

                if (errText != "")
                    return errText;

                XmlDocument doc = new XmlDocument();
                doc.Load(InputPath);

                XmlElement root = doc.DocumentElement;

                XmlNode keywords = root.ChildNodes[0];
                XmlNode urls = root.ChildNodes[1];

                if (_keyword != "")
                {
                    XmlElement keywordNode = doc.CreateElement("keyword");
                    keywordNode.InnerText = _keyword;
                    keywords.AppendChild(keywordNode);
                }

                if (_url != "")
                {
                    XmlElement urlNode = doc.CreateElement("url");
                    urlNode.InnerText = _url;
                    urls.AppendChild(urlNode);
                }

                doc.Save(InputPath);

                return "";
            }
            catch
            {
                return "Khong the add new node";
            }
        }

        public static string RemoveNode(string _url)
        {
            var errText = "";
            try
            {
                errText = IsExist(_url);

                if (errText == "")
                    return errText;

                XmlDocument doc = new XmlDocument();
                doc.Load(InputPath);

                XmlElement root = doc.DocumentElement;

                XmlNode keywords = root.ChildNodes[0];
                XmlNode urls = root.ChildNodes[1];

                for (int i = 0; i < urls.ChildNodes.Count; i++)
                {
                    if(urls.ChildNodes[i].InnerText == _url)
                        urls.ChildNodes[i].ParentNode.RemoveChild(urls.ChildNodes[i]);
                }

                doc.Save(InputPath);

                return "";
            }
            catch
            {
                return "Khong the remove node";
            }
        }

        public static void WriteData(string content, string path)
        {
            using (StreamWriter outfile = new StreamWriter(path))
            {
                outfile.Write(content);
            }
        }
        #endregion
    }
}
